/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.constant;

import java.util.HashMap;
import java.util.Map;

public enum MessageCodeEnum {
    SUCCESS(0, "success"),
    NOT_EFFECTIVE(100020, "not effective"),

    DATA_INSERT_ERROR(100030, "data insert error"),
    DATA_UPDATE_ERROR(100031, "data update error"),
    DUPLICATE_KEY_ERROR(100032, "data duplicate key error"),
    DATA_NOT_FOUND_ERROR(100034, "data query not found error"),
    DATA_REDUNDANCY_ERROR(100035, "data query redundancy error"),

    PARAMETER_ERROR(100040, "parameter error"),
    NOT_SUPPORT_ERROR(100041, "not support error"),

    CALL_CONNECT_ERROR(100050, "remote connect error"),
    CONNECT_NODE_OFFLINE_ERROR(100051, "request connect already offline node error"),
    EXECUTION_ERROR(10060, "execution error"),
    DAG_SCHEDULING_ERROR(10070, "dag scheduling error"),
    COORDINATION_CONNECT_ERROR(10071, "coordination connect error"),
    COORDINATION_COMMAND_EXECUTION_ERROR(10073, "coordination command execution error"),
    STATE_TRANSITION_FAILED(100074, "state transition failed"),
    STATE_UPDATE_ERROR(10075, "state update error"),
    STATE_UPDATE_NOT_EFFECTIVE(10076, "state update not effective"),
    SYSTEM_ERROR(100500, "unknown error"),
    ;

    private final static Map<Integer, MessageCodeEnum> BY_CODE = new HashMap<>();

    static {
        for (MessageCodeEnum e: values()
        ) {
            BY_CODE.put(e.getCode(), e);
        }
    }

    private final int code;
    private final String message;

    MessageCodeEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static MessageCodeEnum valueByCode(int code){
        return BY_CODE.get(code);
    }

    public Boolean isSuccess(){
        return this == MessageCodeEnum.SUCCESS;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}
